package ru.kurganec.ifmo.netland.core;


import org.apache.log4j.Logger;
import ru.kurganec.ifmo.netland.event.NetlandCommandProducer;

import java.io.*;
import java.net.Socket;

/**
 * Created by IntelliJ IDEA.
 * User: oo
 * Date: 12.07.11
 * Time: 21:41
 */
public class NetlandReceiver extends NetlandCommandProducer
        implements Runnable {
    // ===========================================================
    // Constants
    // ===========================================================

    // ===========================================================
    // Fields
    // ===========================================================
    private static Logger logger = Logger.getLogger(NetlandReceiver.class);
    private Socket socketToServer;
    private BufferedReader in;
    private PrintWriter out;
    private boolean connected = false;

    // ===========================================================
    // Constructors
    // ===========================================================
    public NetlandReceiver() {

    }
    // ===========================================================
    // Getter & Setter
    // ===========================================================

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================
    @Override
    public void run() {
        while (true) {
            try {
                String cmd = getCommand();
                if (cmd != null) {
                    fireCommandReceived(cmd);
                }
                Thread.sleep(100);
            } catch (InterruptedException e) {
                continue;
            }
        }
    }
    // ===========================================================
    // Methods
    // ===========================================================
    synchronized private String getCommand() {
        try {
            if (!in.ready()) {
                return  null;
            }
            StringBuilder cmd = new StringBuilder();
            while (in.ready()) {
                int c = in.read();
                cmd.append((char)c);
                if (c == '\n') {
                    return cmd.toString();
                }
            }
            return cmd.toString() + '\n';
        } catch (IOException e) {
            return null;
        }

    }

    synchronized public void sendCommand(String cmd) {
        out.print(cmd + "\n");
        out.flush();
        logger.debug("Command sent: " + cmd);
    }

    public Thread connect() throws IOException {
        socketToServer = new Socket("127.0.0.1", 1539);
        in = new BufferedReader(new InputStreamReader( socketToServer.getInputStream(),"Cp1251"));
        out = new PrintWriter(new OutputStreamWriter(socketToServer.getOutputStream(), "Cp1251"));
        Thread t = new Thread(this);
        t.start();
        fireReceiverConnected(socketToServer.toString());
        return t;
    }
    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================


}
